home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9202 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: news.luc.edu!user
  2. From: VArase@varase.it.luc.edu (Verne Arase)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: GOTO controversy
  5. Date: Fri, 08 Mar 1996 12:42:06 -0600
  6. Organization: LUMC
  7. Message-ID: <AD65DB9E9668BB9B8@mcdiala11.it.luc.edu>
  8. References: <rcshlds.1.000A6705@mailserv.mta.ca> <Dn8pJ8.nqs@emi.net> <4grt4e$8fg@goanna.cs.rmit.EDU.AU> <4hl8mt$4po@newshost.cyberramp.net> <DnwCxp.84C@clw.cs.man.ac.uk>
  9. NNTP-Posting-Host: 147.126.240.111
  10.  
  11. In article <DnwCxp.84C@clw.cs.man.ac.uk>,
  12. chl@clw.cs.man.ac.uk (Charles Lindsey) wrote:
  13.  
  14.  >In <4hl8mt$4po@newshost.cyberramp.net> sinan@cyberramp.net (John Noland)
  15. writes:
  16.  >
  17.  >>>Robert C Shields (rcshlds@mailserv.mta.ca) wrote:
  18.  >>>Example for a good use of goto:
  19.  >>>---                    ????
  20.  
  21. >>>------------------------------------------------------------------------------
  22.  >>>   HEV    hev1, hev2, hev3;     /* Event semaphores */
  23.  >>>   HMTX   hmtx;                 /* Mutex semaphore  */ 
  24.  >>>   void  *ptr;              
  25.  >
  26.  >>>   if (!DosCreateEventSem(0, &hev1, 0, FALSE))
  27.  >>>       goto hev1_failed;
  28.  >>>
  29.  >>>   if (!DosCreateEventSem(0, &hev2, 0, FALSE))
  30.  >>>       goto hev2_failed;
  31.  >
  32.  >>>   if (!DosCreateEventSem(0, &hev3, 0, FALSE))
  33.  >>>       goto hev3_failed;
  34.  >>>
  35.  >>>   if (!DosCreateMutexSem(0, &hmtx, 0, FALSE))
  36.  >>>       goto hmtx_failed;
  37.  >>>
  38.  >>>   if ((ptr = malloc(SOME_SIZE)) == NULL)
  39.  >>>       goto malloc_failed;
  40.  >>>
  41.  >>>   /* Do some stuff here */
  42.  >>>   return TRUE; /* We did okay */
  43.  >>>
  44.  >>>malloc_failed:
  45.  >>>   DosCloseMutexSem(hmtx);
  46.  >>>hmtx_failed:
  47.  >>>   DosCloseEventSem(hev3);
  48.  >>>hev3_failed:
  49.  >>>   DosCloseEventSem(hev2);
  50.  >>>hev2_failed:
  51.  >>>   DosCloseEventSem(hev1);
  52.  >>>hev1_failed:
  53.  >>>   return FALSE;
  54.  >
  55.  >
  56.  >>The above is NOT an example of a good use for the goto statement. 
  57.  >
  58.  >Well that is what I thougut when I first saw the example, and I was about
  59. to
  60.  >dash off an article like you have done. But then I looked again and saw
  61.  >*exactly* what it was doing.
  62.  >
  63.  >So if you believe there is a better way, please can we see it?
  64.  
  65. How about:
  66.  
  67.    HEV   hev1,hev2,hev3;          /* Event semaphores */
  68.    HMTX  hmtx;                    /* Mutex semaphore */
  69.    void  *ptr;
  70.  
  71.    if (DosCreateEventSem(0,&hev1,0,FALSE)) {
  72.       if (DosCreateEventSem(0,&hev2,0,FALSE)) {
  73.          if (DosCreateEventSem(0,&hev3,0,FALSE)) {
  74.             if (DosCreateMutexSem(0,&hmtx,0,FALSE)) {
  75.                if (ptr=malloc(SOME_SIZE))
  76.                   return TRUE;
  77.                DosCloseMutexSem(htmx);
  78.                }
  79.             DosCloseEventSem(hev3);
  80.             }
  81.          DosCloseEventSem(hev2);
  82.          }
  83.       DosCloseEventSem(hev1);
  84.       }
  85.    return FALSE;
  86.  
  87. BTW, I've removed comp.lang.pl1 and comp.lang.misc from this thread; I call
  88. on others to do likewise.
  89.  
  90. ---
  91. The above are my own opinions, and not those of my employer.
  92.